package models

import (
	db2 "go-barrage/db"
	"go-barrage/vo"
	"log"
	"time"
)

type VideoInfo struct {
	Id           int64     `json:"id"`
	UserId       int64     `json:"userId"`
	VideoShortId string    `json:"videoShortId"`
	VideoUrl     string    `json:"videoUrl"`
	Title        string    `json:"title"`
	MediaPreview string    `json:"mediaPreview"`
	DelFlag      bool      `json:"delFlag"`
	UpName       string    `json:"upName"`
	CreateTime   time.Time `json:"createTime"`
	UpdateTime   time.Time `json:"updateTime"`
}

func (VideoInfo) TableName() string {
	return "t_video_info"
}

func GetAllVideoInfo() *[]VideoInfo {
	var videoInfos []VideoInfo
	db := db2.GetMysql()
	err := db.Where("del_flag = 0").Find(&videoInfos).Error
	if err != nil {
		log.Println(err)
	}
	return &videoInfos
}

func GetVideoInfoById(videoShortId string) vo.VideoInfoVo {
	var videoInfo VideoInfo
	db := db2.GetMysql()
	err := db.Debug().Where("video_short_id = ? and del_flag = 0", videoShortId).Find(&videoInfo).Error
	if err != nil {
		log.Println(err)
	}

	user := GetUserInfoById(videoInfo.UserId)

	videoVo := vo.VideoInfoVo{
		Author:          user.UserName,
		Title:           videoInfo.Title,
		VideoUrl:        videoInfo.VideoUrl,
		VideoCreateTime: videoInfo.CreateTime,
	}

	return videoVo
}

func GetRecommendedVideo(num int) []vo.VideoInfoVo {
	var videoInfos []VideoInfo
	db := db2.GetMysql()
	err := db.Debug().Where("del_flag = 0 ORDER BY rand() limit ?", num).Find(&videoInfos).Error
	if err != nil {
		log.Println(err)
	}

	var videoInfoVos []vo.VideoInfoVo
	for _, info := range videoInfos {
		infoVo := vo.VideoInfoVo{
			Title:        info.Title,
			VideoShortId: info.VideoShortId,
			VideoUrl:     info.VideoUrl,
			MediaPreview: info.MediaPreview,
		}
		videoInfoVos = append(videoInfoVos, infoVo)
	}

	return videoInfoVos
}
